package com.crm.config.mybatis;

import cn.hutool.core.lang.Console;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.crm.entity.CrmUser;
import com.crm.util.DateUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author junxian.dou
 * @Package：com.crm.config.mybatis
 * @Project：springboot-crm
 * @name：CustomMetaObjectHandler
 * @Date：2024/12/4 17:40
 * @Filename：CustomMetaObjectHandler
 * @verson 1.0.0
 * @desc
 */

public class CustomMetaObjectHandler implements MetaObjectHandler {


    @Override
    public void insertFill(MetaObject metaObject) {
        CrmUser principal = (CrmUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        strictInsertFill(metaObject,"createTime",String.class, DateUtils.formateDateTime(new Date()));
        strictInsertFill(metaObject,"createBy",String.class, principal.getId());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        CrmUser principal = (CrmUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        this.setFieldValByName("editTime", DateUtils.formateDateTime(new Date()),metaObject);
        this.setFieldValByName("editBy", principal.getId(),metaObject);
    }
}
